package net.tools;

import net.base.BaseDao;
import net.base.Tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jxw
 * Date: 12-2-24
 * Time: 上午10:00
 * To change this template use File | Settings | File Templates.
 */
public class ExcelUtil {
    public BaseDao dao;
    public ExcelUtil(){
        dao = (BaseDao)BeansHelp.getBeanInstance("baseDao");
    }
    /**
     * 导出excel
     * @param request
     */
    public void exportExcel(HttpServletRequest request, HttpServletResponse response){
        Map map = Tools.parserRequest(request);
        String sqlid = (String) map.get("SQLID_EXPORT");

        try {
            int rowsCount = Integer.parseInt(map.get("ExportMaxRows").toString());
            String title = (String)map.get("TITLE");

            map.put("START", 0);
            map.put("END", rowsCount);
            List list = dao.getList(sqlid,map);

            String heads =map.get("HeadsName").toString();

            String[] headsName = heads.split(",");

            String properies = map.get("ProperiesName").toString();
            String[] properiesName = properies.split(",");
            int[] colswidth = null;
            try{
                String cols = map.get("ColsWidth").toString();
                String[] colswidth_str = cols.split(",");
                colswidth = new int[colswidth_str.length];
                for(int i=0;i<colswidth_str.length;i++){
                    colswidth[i] = (Integer.parseInt(colswidth_str[i]));
                }
            }catch(Exception e){
                colswidth = null;
            }
            String[] aligns = ((String)map.get("Aligns")).split(",");

            GridExportUtil gridexport = new GridExportUtil(request,response);
            gridexport.exportXLS(title, list, properiesName, headsName,colswidth, aligns,Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
